# -*- coding: utf-8 -*-

import os
import sys

if __name__ == "__main__":
    currentdir = os.path.dirname( os.path.abspath( sys.argv[0] ) )
    basedir = os.path.abspath( os.path.join( currentdir, ".." ) )
    pythondir = basedir
    sys.path.insert( 0, pythondir )
    pass

from logger import get_file_logger
from datetime import timedelta
import random
from picklerpc.msghandler import RequestHandler
from picklerpc.sendmsg import sendmsg
from picklerpc import Server
from time import sleep, time
from service import default_request_timeout, default_niceness_level

__all__ = ['HashHandler', 'HashServer', 'HashRequest', 'HashManager']

#---------------------------------------------

class HashHandler(RequestHandler):
    """
    Do some tasks, maybe in subprocess
    """
    
    def _request_handle(self, data):
        """
        @param data: simple object data
        @type: dict
        
        @return: python simple object data, jsonable via json.dumps
        @rtype: mixed
        """
        result = False
        
        if not (type(data) is dict):
            return result

        if data["task"] == "ping":
            return self.server.ping()
        if data["task"] == "quit":
            return self.server.quit()
        if data["task"] == "hash":
            data = data["data"]
            return self.server.hash(
                data["file_id"],
                data["device"],
                data["file_size"],
                data["full_path"]
            )
        return result
    
    pass

#---------------------------------------------

class HashServer(Server):
    
    queue = None
    stop_event = None
    task_service_address = None
    _worker_thread = None

    log = None

    def server_activate(self):
        try:
            # Python 3
            from queue import Queue
        except ImportError:
            # Python 2
            from Queue import Queue

        from threading import Thread, Event

        self.log = get_file_logger("HashServer")

        self.queue = Queue()
        self.stop_event = Event()
        self._worker_thread = Thread(target=self._worker)
        self._worker_thread.start()
        return Server.server_activate(self)

    def ping(self):
        return "pong"

    def _worker ( self ):

        self.log.info("START: %s" % (self.server_address,))

        import hashlib
        from service.task import TaskManager
        from service.statistics import StatisticsDirectRequest
        from service.sqlite3_database import Sqlite3DatabaseDirectRequest

        task_manager = TaskManager(self.task_service_address)

        stats_req = task_manager.get_request().getStatistics()
        while not stats_req.ping():
            sleep(0.1)
        addr = stats_req.get_address()
        stats_req = StatisticsDirectRequest(addr)


        db_req = task_manager.get_request().getDatabase()
        while not db_req.ping():
            sleep(0.1)
        addr = db_req.get_address()
        db_req = Sqlite3DatabaseDirectRequest(addr)


        hash_func = db_req.query("get_option", ("hash_function",))

        while not self.stop_event.isSet():

            try:
                data = self.queue.get_nowait()
            except Exception:
                data = None

            if data is None:
                sleep(0.1)
                continue

            file_id, device, file_size, full_path = data

            f = None
            shash = None
            try:
                stats_req.set_hashing( device, full_path, file_size, 0, time() )

                alg = hashlib.new(hash_func)


                st = os.lstat(full_path)
                read_chunk = st.st_blksize * 32

                f = open( full_path, "rb" )

                rs = 0
                t_last = time()
                while 1:
                    if self.stop_event.isSet():
                        break
                    rd = f.read( read_chunk )
                    rds = len(rd)
                    if rds > 0:
                        alg.update(rd)
                        rs += rds
                        if time() - t_last >= 0.1:
                            t_last = time()
                            stats_req.inc("real_readed_size", rs )
                            stats_req.inc("readed_size", rs )
                            stats_req.inc_hashing(device, rs )
                            rs = 0
                    else:
                        break
                f.close()
                f = None
                stats_req.inc("real_readed_size", int(rs) )
                stats_req.inc("readed_size", int(rs) )
                stats_req.inc_hashing(device, int(rs) )
                shash = alg.hexdigest()
            except Exception as e:
                import traceback
                self.log.error("%s" % (e,))
                self.log.debug("ERROR!\n File: '%s'\n%s" % (
                    full_path, traceback.format_exc()
                ))
                if f:
                    f.close()

            if shash:
                hitem = db_req.query("find_hash", ( shash, ))
                if hitem:
                    stats_req.inc("nonunique_count", 1)
                    stats_req.inc("nonunique_size", file_size)
                    stats_req.inc("nonunique_count_now", 1)
                    stats_req.inc("nonunique_size_now", file_size)

                    db_req.query("link_hash", ( file_id, hitem["id"],), False)
                else:
                    stats_req.inc("unique_count", 1)
                    hash_id = db_req.query("insert_hash", ( shash, ), True)
                    db_req.query("link_hash", ( file_id, hash_id, ), False)

                stats_req.inc("hashed_count", 1)
            else:
                stats_req.inc("errors_count", 1)

            stats_req.inc("files_processed", 1)

            self.queue.task_done()


        self.log.info("STOP: %s" % (self.server_address,))

        self.stop_event.clear()
        return

    def hash(self, file_id, device, file_size, full_path):
        self.queue.put_nowait((file_id, device, file_size, full_path,))
        return True

    def quit(self):
        self.log.info("quit event")

        import signal

        self.log.debug("send SIGINT to self")
        os.kill( os.getpid(), signal.SIGINT )

        return True


    def terminate_self(self, signum, frame):
        self.log.debug("got SHUTDOWN signal")
        t_begin = time()

        # Clean Queue

        while True:
            try:
                data = self.queue.get_nowait()
            except Exception:
                data = None

            if data is None:
                break

            self.queue.task_done()


        self.stop_event.set()
        self.stop_event.wait()
        self.server_close()
        t_end = time()
        self.log.info("SHUTDOWN in %s sec" % (timedelta(seconds=t_end - t_begin),))
        return

    pass

#---------------------------------------------


class HashRequest(object):

    manager = None

    def __init__(self, manager):
        self.manager = manager
        pass

    def hash(self, file_id, device, file_size, full_path):
        return self.manager.sendmsg( device, "hash", {
            "file_id" : file_id,
            "device" : device,
            "file_size" : file_size,
            "full_path" : full_path
        })

    def ping(self, device):
        return self.manager.sendmsg(device, "ping", {}, 0.1)

    def ping_all(self):
        return self.manager.ping_all()

    def quit(self):
        return self.manager.quit_all()

    def get_address(self, device):
        return self.manager.get_address(device)

    pass


class HashDirectRequest(object):

    address = None

    def __init__(self, service_address):
        self.address = service_address
        self.log = get_file_logger("HashDirectRequest")
        pass

    def sendmsg(self, task, data, timeout=default_request_timeout):
        try:
            result = sendmsg( self.address, {"task":task, "data":data}, timeout=timeout )
        except Exception as e:
            result = None
            self.log.error("SOCKET: %s" % ( e,))
            self.log.debug("FAIL SEND MESSAGE: task=%s, data=%r" % (task, data,))
        return result

    def hash(self, file_id, device, file_size, full_path):
        return self.sendmsg( "hash", {
            "file_id" : file_id,
            "device" : device,
            "file_size" : file_size,
            "full_path" : full_path
        })

    def ping(self, device):
        return self.sendmsg("ping", {}, 0.1)

    def quit(self):
        return self.sendmsg("quit", {}, 0.1)

    pass


#---------------------------------------------

class HashManager(object):

    host = "127.0.1.6"
    port_min = 10000
    port_max = 20000
    ports = None
    request = None

    task_service_address = None

    log = None

    def __init__(self, task_service_address):
        self.ports = {}
        self.task_service_address = task_service_address
        self.log = get_file_logger("HashManager")
        pass

    def start_service(self, device):

        device = str(device)

        runing = False
        if self.ports.get(device) is None:

            port = random.randint(self.port_min, self.port_max)

            self.ports[device] = port

            from archive import which
            from subprocess import Popen

            Popen([which("python3"), "service/hash.py",
                   "--host", "%s" % self.host,
                   "--port", "%s" % port,
                   "--task-service", "%s:%s" % self.task_service_address
            ])

            # WAIT WHILE SERVICE STARTS

            pings = 100
            t_begin = time()
            while not runing:

                pong = self.get_request().ping(device)

                if pong == "pong":
                    runing = True

                pings -= 1
                if pings <= 0:
                    break

            if pings <= 0:
                del self.ports[device]
                self.log.error("FAIL TO START service on (%s, %s) in %s sec" % (
                    self.host, port, timedelta(seconds=time() - t_begin),))
            else:
                self.log.info("STARTED service on (%s, %s) in %s sec" % (
                    self.host, port, timedelta(seconds=time() - t_begin),))

        return runing

    def sendmsg(self, device, task, data, timeout=default_request_timeout):
        device = str(device)
        port = self.ports[device]
        SERVER_ADDRESS = (self.host, port,)

        try:
            result = sendmsg( SERVER_ADDRESS, {"task":task, "data":data}, timeout=timeout )
        except Exception as e:
            result = None
            self.log.error("SOCKET %r" % (e,))
            self.log.debug("FAIL SEND MESSAGE: task=%s, data=%r" % (task, data,))
        return result

    def get_address(self, device):
        device = str(device)
        port = self.ports[device]
        SERVER_ADDRESS = (self.host, port,)
        return SERVER_ADDRESS

    def quit_all(self):
        result = True
        ports = self.ports.copy()
        for device, port in ports.items():
            ret = self.sendmsg(device, "quit", {}, 0.1)
            if ret is None:
                result = False
            else:
                del self.ports[ device ]
        return result

    def ping_all(self):
        result = False
        ports = self.ports.copy()
        for device, port in ports.items():
            ret = self.get_request().ping(device)
            if ret == "pong":
                result = True
        return result

    def get_request(self):
        if not self.request:
            self.request = HashRequest(self)
        return self.request

    pass


#---------------------------------------------

def run_service(host, port, task_service_address):

    if hasattr(os, "nice"):
        os.nice(default_niceness_level)

    server = None
    e_code = 0
    try:
        SERVER_ADDRESS = (host, int(port),)

        server = HashServer(SERVER_ADDRESS, HashHandler, bind_and_activate=False)
        if task_service_address:
            host, port = task_service_address.split(':')
            server.task_service_address = (host, int(port),)

        import signal

        signal.signal(signal.SIGINT, server.terminate_self)

        server.server_bind()
        server.server_activate()
        server.serve_forever()
    except Exception as e:
        e_code = 1
        if server and server.log:
            server.log.error("run_service: ERROR=%r" % (e,))
        pass

    return e_code

#---------------------------------------------

def launch():

    from argparse import ArgumentParser

    parser = ArgumentParser(description='Run file hashing service.')
    parser.add_argument('--host', dest='host', action='store', type=str,
        help='Listen on TCP address')
    parser.add_argument('--port', dest='port', action='store', type=int,
        help='Listen on TCP port')
    parser.add_argument('--task-service', dest='task_service', action='store', type=str,
        help='Task service listen on TCP address')

    args = parser.parse_args()
    return run_service(args.host, args.port, args.task_service)

if __name__ == "__main__":
    sys.exit(launch())
